package com.zhf.servlet;

import com.zhf.bean.Grade;
import com.zhf.bean.Student;
import com.zhf.service.GradeService;
import com.zhf.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/25 19:54
 */
@WebServlet(name = "AddGradeServlet" ,urlPatterns = "/addgrade")
public class AddGradeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        request.setCharacterEncoding("utf-8");
        String classname = (String)request.getParameter("classname");
        String grade = (String)request.getParameter("grade");
        String adviser = (String)request.getParameter("adviser");
        String slogan = (String)request.getParameter("slogan");
        int classsize = Integer.parseInt(request.getParameter("classsize"));

        Grade grade1 = new Grade(classname, grade, adviser, slogan, classsize);
        GradeService gradeService = new GradeService();
        //判断该班级是否存在
        if (gradeService.getGrade(classname)==null){
            //不存在则添加
            boolean b = gradeService.addGrade(grade1);
            //将修改结果写入request转发到该页面返回
            request.setAttribute("result", b ? "seccess" : "fail");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("addgrade.jsp");
            requestDispatcher.forward(request,response);

        }else {
            //服务器跳转request共享
            request.setAttribute("error","studenterror");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("addgrade.jsp");
            requestDispatcher.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
